import sys
import pygame
import os
from pygame.locals import *

pygame.init()

black = 0, 0, 0
white = 255, 255, 255

def load_image(name):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', name
        raise SystemExit, message
    image = image.convert()
    image.set_colorkey(black, pygame.RLEACCEL)
    return image, image.get_rect()


size = width, height = 320, 240
black = 0, 0, 0
white = 255, 255, 255
green = 0, 255, 0

class MainHero(pygame.sprite.Sprite):
    """Main hero character"""
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = load_image('logo.jpg')
        self.movedir  = self.NOMOVE
        self.rect.topleft = (10,10)

    NOMOVE = (0,0)
    LEFT   = (-5,0)
    RIGHT  = (5,0)
    UP     = (0,-5)
    DOWN   = (0,5)

    def update(self):
        "move the hero to direction"
        if self.movedir != self.NOMOVE:
            self.rect = self.rect.move(self.movedir)
            print "Move to ",self.movedir
            print "position is ",self.rect.topleft
            self.movedir = self.NOMOVE

    def move(self,direction):
        print "move to direction"
        self.movedir = direction
        
                


screen = pygame.display.set_mode(size)
hero = MainHero()
allsprites = pygame.sprite.RenderPlain((hero))
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == K_LEFT:
                hero.move(MainHero.LEFT)
            if event.key == K_RIGHT:
                hero.move(MainHero.RIGHT)
            if event.key == K_UP:
                hero.move(MainHero.UP)
            if event.key == K_DOWN:
                hero.move(MainHero.DOWN)
    allsprites.update()
    screen.fill(green)
    allsprites.draw(screen)
    pygame.display.flip()
    pygame.time.delay(60)